package com.cqjtu.eecs.utils.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqjtu.eecs.entity.GraduateRequirementTemplate;
import com.cqjtu.eecs.entity.GraduateRequirementTrainingGoalTemplate;
import com.cqjtu.eecs.entity.TrainingGoalTemplate;
import com.cqjtu.eecs.entity.dto.GraduateRequirementTrainingGoalTemplateDTO;
import com.cqjtu.eecs.entity.dto.TrainingProjectDTO;
import com.cqjtu.eecs.service.IGraduateRequirementTemplateService;
import com.cqjtu.eecs.service.IGraduateRequirementTrainingGoalTemplateService;
import com.cqjtu.eecs.service.ITrainingGoalTemplateService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class GraduateRequirementTrainingGoalTemplateListener extends AnalysisEventListener<GraduateRequirementTrainingGoalTemplateDTO> {

    private ITrainingGoalTemplateService trainingGoalTemplateService;
    private IGraduateRequirementTemplateService graduateRequirementTemplateService;
    private IGraduateRequirementTrainingGoalTemplateService graduateRequirementTrainingGoalTemplateService;
    private TrainingProjectDTO trainingProjectDTO;
    private List<GraduateRequirementTrainingGoalTemplateDTO> list;

    public GraduateRequirementTrainingGoalTemplateListener(
            ITrainingGoalTemplateService trainingGoalTemplateService,
            IGraduateRequirementTemplateService graduateRequirementTemplateService,
            IGraduateRequirementTrainingGoalTemplateService graduateRequirementTrainingGoalTemplateService,
            TrainingProjectDTO trainingProjectDTO
    ){
        this.graduateRequirementTemplateService = graduateRequirementTemplateService;
        this.graduateRequirementTrainingGoalTemplateService = graduateRequirementTrainingGoalTemplateService;
        this.trainingGoalTemplateService = trainingGoalTemplateService;
        this.trainingProjectDTO = trainingProjectDTO;
        list = new ArrayList<>();
    }


    @Override
    public void invoke(GraduateRequirementTrainingGoalTemplateDTO graduateRequirementTrainingGoalTemplateDTO, AnalysisContext analysisContext) {
        if (graduateRequirementTrainingGoalTemplateDTO.getGraduateRequirementId() != null)
            list.add(graduateRequirementTrainingGoalTemplateDTO);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 根据专业代码和版本编号查询对应的毕业要求模板
        List<GraduateRequirementTemplate> graduateRequirementTemplates = graduateRequirementTemplateService.list(new LambdaQueryWrapper<GraduateRequirementTemplate>()
                .eq(GraduateRequirementTemplate::getMajorCode, trainingProjectDTO.getMajorCode())
                .eq(GraduateRequirementTemplate::getVersionCode, trainingProjectDTO.getVersionCode()));
        // 根据专业代码和版本编号查询对应的培养目标模板
        List<TrainingGoalTemplate> trainingGoalTemplates = trainingGoalTemplateService.list(new LambdaQueryWrapper<TrainingGoalTemplate>()
                .eq(TrainingGoalTemplate::getMajorCode, trainingProjectDTO.getMajorCode())
                .eq(TrainingGoalTemplate::getVersionCode, trainingProjectDTO.getVersionCode()));
        // 做map映射
        Map<String, Integer> graduateRequirementTemplateMap = graduateRequirementTemplates
                .stream()
                .collect(Collectors.toMap(GraduateRequirementTemplate::getGraduateRequirementCode, GraduateRequirementTemplate::getGraduateRequirementId));
        Map<String, Integer> trainingGoalTemplateMap = trainingGoalTemplates
                .stream()
                .collect(Collectors.toMap(TrainingGoalTemplate::getTrainingGoalCode, TrainingGoalTemplate::getTrainingGoalId));

        // 生成对应的支持对象
        List<GraduateRequirementTrainingGoalTemplate> graduateRequirementTrainingGoalTemplates = new ArrayList<>();
        list.forEach(item->{
            graduateRequirementTrainingGoalTemplates
                    .add(new GraduateRequirementTrainingGoalTemplate(trainingGoalTemplateMap.get(item.getTrainingGoalId()), graduateRequirementTemplateMap.get(item.getGraduateRequirementId()), true));
        });
        // 插入到数据库
        graduateRequirementTrainingGoalTemplateService.saveBatch(graduateRequirementTrainingGoalTemplates);
    }
}
